package com.wuhunyu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.el.parser.AstDotSuffix;
import com.wuhunyu.commonutils.check.Check;
import com.wuhunyu.commonutils.query.CommonQuery;
import com.wuhunyu.commonutils.result.CommonResult;
import com.wuhunyu.entities.EduCourse;
import com.wuhunyu.entities.EduTeacher;
import com.wuhunyu.mapper.EduCourseMapper;
import com.wuhunyu.mapper.EduTeacherMapper;
import com.wuhunyu.query.EduTeacherQuery;
import com.wuhunyu.service.EduTeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wuhunyu.servicebase.exception.Assert;
import com.wuhunyu.utils.PageUtil;
import net.bytebuddy.implementation.auxiliary.MethodCallProxy;
import org.apache.ibatis.cache.CacheKey;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author wuhunyu
 * @since 2021-01-12
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {

    @Resource
    private EduTeacherMapper eduTeacherMapper;

    @Resource
    private EduCourseMapper eduCourseMapper;

    @Override
    public CommonResult selectAllTeacher(EduTeacherQuery eduTeacherQuery) {
        Page<EduTeacher> eduTeacherPage = new Page<>(eduTeacherQuery.getCurrent(), eduTeacherQuery.getLimit());
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        // name
        queryWrapper.likeRight(Check.isNotBlank(eduTeacherQuery.getName()), "name", eduTeacherQuery.getName());
        // level
        queryWrapper.eq(Check.isNotBlank(eduTeacherQuery.getLevel()), "level", eduTeacherQuery.getLevel());
        // begin
        queryWrapper.ge(Check.isNotBlank(eduTeacherQuery.getBegin()), "gmt_create", eduTeacherQuery.getBegin());
        // end
        queryWrapper.le(Check.isNotBlank(eduTeacherQuery.getEnd()), "gmt_create", eduTeacherQuery.getEnd());
        // 倒序
        queryWrapper.orderByDesc("gmt_modified");
        Page<EduTeacher> page = eduTeacherMapper.selectPage(eduTeacherPage, queryWrapper);
        Map<String, Object> map = new HashMap<>(16);
        map.put("items", page.getRecords());
        map.put("total", page.getTotal());
        return CommonResult.success("查询全部讲师成功", map);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommonResult deleteTeacher(String id) {
        int result = eduTeacherMapper.deleteById(id);
        if (result != 1) {
            return CommonResult.error("逻辑删除讲师失败");
        }
        return CommonResult.success("逻辑删除讲师成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommonResult insertTeacher(EduTeacher eduTeacher) {
        // name
        Assert.isRight(Check.isBlank(eduTeacher.getName()), "教师名称不能为空");
        // level
        Assert.isRight(Check.isBlank(!(eduTeacher.getLevel()==1 || eduTeacher.getLevel()==2)), "教师级别非法");
        int result = eduTeacherMapper.insert(eduTeacher);
        if (result == 0) {
            Assert.isRight(true, "新增教师失败");
            return CommonResult.error("新增教师失败");
        }
        return CommonResult.success("新增教师成功");
    }

    @Override
    public CommonResult selectTeacherById(String id) {
        EduTeacher eduTeacher = eduTeacherMapper.selectById(id);
        Assert.isRight(Check.isBlank(eduTeacher), "查询结果为空");
        HashMap<String, Object> map = new HashMap<>(16);
        map.put("total", 1);
        map.put("items", eduTeacher);
        return CommonResult.success("查询成功", map);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommonResult updateTeacher(EduTeacher eduTeacher) {
        Assert.isRight(Check.isBlank(eduTeacher.getId()), "id不能为空");
        // 查询需要修改的教师是否存在
        EduTeacher teacher = eduTeacherMapper.selectById(eduTeacher.getId());
        Assert.isRight(Check.isBlank(teacher), "修改的对象不存在");
        // 执行修改
        Assert.isRight(Check.isBlank(eduTeacherMapper.updateById(eduTeacher)), "修改失败");
        return CommonResult.success("修改成功");
    }

    @Override
    public CommonResult selectTeacherForSelect() {
        // 查询全部讲师的id和name
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "name");
        // 执行查询操作
        List<EduTeacher> eduTeachers = eduTeacherMapper.selectList(queryWrapper);
        // 校验结果
        Assert.isRight(Check.isBlank(eduTeachers), "查询讲师信息为空");
        Map<String, Object> map = new HashMap<>(16);
        map.put("teachers", eduTeachers);
        return CommonResult.success("查询讲师信息成功", map);
    }

    @Override
    @Cacheable(key = "'teacher'",value = "teachers")
    public CommonResult selectTeachers() {
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        queryWrapper.last("limit 4");
        List<EduTeacher> eduTeachers = eduTeacherMapper.selectList(queryWrapper);
        // 校验结果集
        Assert.isRight(eduTeachers.size() != 4, "查询讲师数据有误");
        // 封装结果集
        HashMap<String, Object> map = new HashMap<>(16);
        map.put("teachers", eduTeachers);
        return CommonResult.success("查询讲师数据成功", map);
    }

    @Override
    public CommonResult selectTeachersForFront(CommonQuery commonQuery) {
        // 参数校验
        Assert.isRight(Check.isBlank(commonQuery), "查询名师参数不能为空");
        Page<EduTeacher> page = new Page<>(commonQuery.getCurrent(), commonQuery.getLimit());
        // 执行查询操作
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        Page<EduTeacher> teachers = eduTeacherMapper.selectPage(page, queryWrapper);
        // 封装结果集
        Map<String, Object> map = new PageUtil<EduTeacher>().generateMap(teachers);
        return CommonResult.success("查询名师信息成功", map);
    }

    @Override
    public CommonResult selectCourseByTeacherId(String teacherId) {
        // 校验参数
        Assert.isRight(Check.isBlank(teacherId), "讲师id不能为空");
        // 查询讲师信息
        EduTeacher teacher = eduTeacherMapper.selectById(teacherId);
        Assert.isRight(Check.isBlank(teacher), "讲师不存在");
        // 查询讲师课程
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("teacher_id", teacherId);
        List<EduCourse> eduCourses = eduCourseMapper.selectList(queryWrapper);
        // 校验结果集
        Assert.isRight(eduCourses == null, "讲师课程为空");
        // 封装结果集
        Map<String, Object> map = new HashMap<>(16);
        map.put("teacher", teacher);
        map.put("courses", eduCourses);
        return CommonResult.success("查询讲师课程成功", map);
    }
}
